package cn.hs.ph.servlet;

import cn.hs.ph.dao.CommodityDao;
import cn.hs.ph.dao.impl.CommodityDaoImpl;
import cn.hs.ph.entity.Commodity;
import cn.hs.ph.util.Result;
import com.alibaba.fastjson.JSON;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/commodity")
public class CommodityServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        CommodityDao commodityDao=new CommodityDaoImpl();
        if (action.equals("selectAll")){
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            String shopName = req.getParameter("shopName");
            String goodsName = req.getParameter("goodsName");
            Map<String,String> map=new HashMap<>();
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            map.put("shopName",shopName);
            map.put("goodsName",goodsName);
            List<Commodity> list = commodityDao.selectAll(map);
            int total = commodityDao.selectCount(map);
            Map resultMap=new HashMap();
            resultMap.put("list",list);
            resultMap.put("total",total);
            Result result= Result.success(resultMap);
            resp.getWriter().write(JSON.toJSONString(result));
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
